let User = require('../model/user')
let result = require('../utils/result')
let format = require('../utils/format.js')
function UserController(){

};

/**
 * 封禁
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
UserController.prototype.userban = async (req,res)=>{
    let uid = req.body.uid;
    User.update({ban:1},{where:{ban:0,uid:uid}}).then(rsp=>{
        res.status(200).json(result(0,'封禁成功',rsp))

    }).catch(err=>{
        res.status(200).json(result(-1,'封禁失败'))

    })
}

/**
 * 解封
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
UserController.prototype.usercban = async (req,res)=>{
    let uid = req.body.uid;
    User.update({ban:0},{where:{ban:1,uid:uid}}).then(rsp=>{
        res.status(200).json(result(0,'解封成功',rsp))

    }).catch(err=>{
        res.status(200).json(result(-1,'解封失败'))

    })
}

UserController.prototype.isban= async (req,res)=>{
    User.findAll({where:{ban:1}}).then(rsp=>{
        res.status(200).json(result(0,'success',rsp))
    })
}

/**
 * 查询所有用户
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
UserController.prototype.findAll = async (req,res)=>{
    let tmp = await User.findAll();
    res.status(200).json(result(0,'success',tmp))
}

UserController.prototype.findUser = async (user,req,res)=>{
    return User.findOne({where:{uid:user}})
}

/**
 * 基本用户信息
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
UserController.prototype.userBaseInfo = async (req,res)=>{
    let uid = req.body.uid;

    User.findOne({where:{uid:uid}}).then(rsp=>{
        if(rsp){

            res.status(200).json(result(0,'success',rsp))
        }else{
            res.status(200).json(result(-1,'获取失败'))
        }
    })
}



/**
 * 用户创建
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
UserController.prototype.createFn = async (req,res)=>{
    User.create({
        openid: req.query.openid,
        nickname: req.query.nickname,
        sex: req.query.sex,
        country: req.query.country,
        headimgurl: req.query.headimgurl,
        username: req.query.username,
        isVerify: req.query.isVerify,
        balance: req.query.balance,
        isDel: req.query.isDel,
        account: req.query.account,
        password: req.query.password,
        ban: req.query.ban,
        createdAt:format.timestamp()
    }).then(Result=>{
        res.status(200).json(result(0,'成功','ok'))
    }).catch(err=>{
        res.status(500).json(result(-1,'失败',err))
    });
}

UserController.prototype.changeFn = async (req,res)=>{
    User.update({type:req.body.type},{where: {uid:req.body.uid}}).then(rsp=>{
        User.findOne({where:{uid:req.body.uid}}).then(user=>{
            res.status(200).json(result(0,'身份切换成功',{user:{
                    uid:user.uid,
                    headimgurl:user.headimgurl,
                    nickname:user.nickname,
                    balance:user.balance,
                    profit:user.profit,
                    type:user.type
                }}))
        })

    }).catch(err=>{
        res.status(405).json(result(-1,err))
    })
}

UserController.prototype.headInfo = async (req,res)=>{
    let uid = req.body.uid;
    let headimgurl = req.body.headimgurl;
    let nickname = req.body.nickname;

    if(uid&&headimgurl){
        User.update({nickname:nickname,headimgurl:headimgurl},{where:{uid:uid}});
        res.status(200).json(result(0,'操作成功'))
    }else{
        res.status(200).json(result(-1,'信息填写不完整'))
    }
}




let userController = new UserController();
module.exports = userController
